import java.util.Scanner;

public class LongestRepeatedSubstring {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个字符串：");
        String input = scanner.nextLine();
        scanner.close();

        String longestRepeatedSubstring = findLongestRepeatedSubstring(input);
        System.out.println("最长的重复字符串是：" + longestRepeatedSubstring);
    }

    public static String findLongestRepeatedSubstring(String str) {
        if (str == null || str.length() < 2) {
            return "";
        }

        String longestRepeatedSubstring = "";
        int maxLength = 0;

        for (int i = 0; i < str.length(); i++) {
            for (int j = i + 1; j < str.length(); j++) {
                int length = Math.min(str.length() - i, str.length() - j);
                String substring1 = str.substring(i, i + length);
                String substring2 = str.substring(j, j + length);

                int k;
                for (k = 0; k < length; k++) {
                    if (substring1.charAt(k) != substring2.charAt(k)) {
                        break;
                    }
                }

                if (k > maxLength) {
                    maxLength = k;
                    longestRepeatedSubstring = substring1.substring(0, k);
                }
            }
        }

        return longestRepeatedSubstring;
    }
}
